<?php

namespace Admin\Controller;

class UserController extends AdminbaseController {

    protected $userModel, $roleModel, $roleUserModel,$shopModel,$adminShop;

    function _initialize() {
        parent::_initialize();
        $this->userModel = D("admin");
        $this->roleModel = D("AdminRole");
        $this->roleUserModel = D("AdminRoleUser");
    }

    /**
     *  管理员管理，有add添加，edit修改，delete删除
     */
    function index() {
        
        $map['s.shop_id'] = array('in', $this->shopIds);
        
        $count = $this->userModel->count();
        $page = $this->page($count, 20);
        $users = $this->userModel
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();

        $this->assign("page", $page->show('Admin'));
        $this->assign("users", $users);
        $this->display();
    }

    /**
     *  添加管理员
     */
    function add() {
        $roles = $this->roleModel->where("status=1")->order("id desc")->select();
        
        $this->assign("roles", $roles);
        $this->display();
    }

    /**
     *  添加管理员
     */
    function add_post() {
        if (IS_POST) {
            if (!empty($_POST['role_id']) && is_array($_POST['role_id'])) {
                $roleIds = I('post.role_id');
                $name = I('post.name');
                $password = I('post.password');
                
                $user = $this->userModel->where(array('name' => $name))->find();
                
                if(!isNullOrEmpty($user)) {
                    $this->error("该用户名已存在");
                }
                
                $data['name'] = $name;
                $data['password'] = md5($password);
                $data['create_time'] = getCurrentDate();
                
                $userId = $this->userModel->add($data);
                
                $userRole = $this->roleUserModel->where(array("user_id" => $userId))->find();
                if (!isNullOrEmpty($userRole)) {
                    $this->error("该用户已存在角色，请编辑修改!");
                }

                foreach ($roleIds as $roleId) {
                    $this->roleUserModel->add(array("role_id" => $roleId, "user_id" => $userId));
                }
                $this->success("添加成功", U("user/index"));
            } else {
                $this->error("请为此用户指定角色!");
            }
        }
    }

    /**
     *  编辑管理员
     */
    function edit() {
        $userId = intval(I("get.id"));
        $roles = $this->roleModel->order("id desc")->select();
        $role_ids = $this->roleUserModel->where(array("user_id" => $userId))->getField("role_id", true);
        $user = $this->userModel->where(array("user_id" => $userId))->find();
        
        $this->assign("roles", $roles);
        $this->assign("role_ids", $role_ids);
        $this->assign('user', $user);
        $this->display();
    }

    /**
     *  编辑管理员
     */
    function edit_post() {
        if (IS_POST) {
            if (!empty($_POST['role_id']) && is_array($_POST['role_id'])) {
                $role_ids = I('post.role_id');
                $userId = I('post.user_id');
                $name = I('post.name');
                $password = I('post.password');
                
                $user = $this->userModel->where(array('user_id'=>$userId))->find();
                
                $existMap['user_id'] = array('neq', $userId);
                $existMap['name'] = $name;
                
                $existUser = $this->userModel->where($existMap)->find();
                
                if(!isNullOrEmpty($existUser)) {
                    $this->error("已经存在该用户名！");
                }
                
                $user['name'] = $name;
                $user['password'] = md5($password);
                
                $this->userModel->where(array('user_id'=>$userId))->save($user);
                
                $this->roleUserModel->where(array("user_id" => $userId))->delete();

                foreach ($role_ids as $role_id) {
                    $this->roleUserModel->add(array("role_id" => $role_id, "user_id" => $userId));
                }
                
                $this->success("保存成功！", U("user/index"));
            } else {
                $this->error("请为此用户指定角色！");
            }
        }
    }

    /**
    *  删除
    */
   function delete() {
       $userId = intval(I("get.id"));
       $this->roleUserModel->where(array("user_id" => $userId))->delete();
       $this->userModel->where(array("user_id" => $userId))->delete();
       $this->success("删除成功！");
   }
    
}